package com.smkj.kaoqin.modules.teacher.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.smkj.kaoqin.common.enumeration.GlobalExceptionEnum;
import com.smkj.kaoqin.common.result.VResponse;
import com.smkj.kaoqin.common.utils.BaseContext;
import com.smkj.kaoqin.modules.common.pojo.db.Classes;
import com.smkj.kaoqin.modules.teacher.mapper.TClassesMapper;
import com.smkj.kaoqin.modules.teacher.pojo.db.Teacher;
import com.smkj.kaoqin.modules.teacher.pojo.dto.ClassesDTO;
import com.smkj.kaoqin.modules.teacher.service.ITClassesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yztyear
 * @since 2024-09-11
 */
@Service
public class TClassesServiceImpl extends ServiceImpl<TClassesMapper, Classes> implements ITClassesService {

    @Override
    public VResponse<String> addClasses(ClassesDTO classesDTO) {
        String teacherName = classesDTO.getTeacherName();
        Long account = Long.valueOf(BaseContext.getInfo());

        // 验证教师账号是否存在,前端输入的教师名称是否和注册教师账号一样
        Teacher teacher = Db.lambdaQuery(Teacher.class)
                .eq( Teacher::getId, account)
                .one();
        if (BeanUtil.isEmpty(teacher)||!teacherName.equals(teacher.getName())) {
            return VResponse.result(GlobalExceptionEnum.ERROP_TEACHER_NAME);
        }

        Classes classes = BeanUtil.copyProperties(classesDTO, Classes.class);
        // 设置教师账号
        classes.setTeacherId(account);

        // 新增班级
        save(classes);

        // TODO 生成二维码
        return VResponse.success("创建成功");
    }
}
